package com.aaa.dynamic;


import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
/**
 * @Description:
 * @author:LiWei
 * @date:2022年11月09日15:38
 */
public class ProxyFactory implements MethodInterceptor {
    //被代理的对象
    private Object target;
    public ProxyFactory(Object target) {
        this.target = target;
    }

    //获取代理对象
    public Object getInstance(){
        //生成代理对象
        Enhancer enhancer=new Enhancer();
        //指定代理类父类
        enhancer.setSuperclass(target.getClass());
        //回调方法
        enhancer.setCallback(this);
        return enhancer.create();

    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        intro();
        //调用被代理对象的方法
        Object result = method.invoke(target, objects);
        watch();
        return result;
    }
    public void watch(){
        System.out.println("看房");
    }
    public void intro(){
        System.out.println("房源介绍");
    }
}